﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DiyBook.Models;

namespace DiyBook.Controllers
{
    public class BorrowController : Controller
    {
        //
        // GET: /Borrow/
        BooksDB db = new BooksDB();
        public static string Bname;

        public ActionResult Index()
        {
            var borrows = from m in db.Borrows
                           select m;
            return View(borrows.ToList());
        }

        public ActionResult BSearch()
        {
            Bname = Request.Form["Bname"];
            if (Bname != null)
            {
                return RedirectToAction("BSearchResult");
            }
            else
                return View();
        }

        public ActionResult BSearchResult()
        {
            ViewData["Bname"]="你查找的书名:"+Bname;
            var searchs = from m in db.Books
                          where Bname == m.bookName
                          select m;
            return View(searchs.ToList());
        }

        public ActionResult Bborrow(int id)
        {
            Book b1 = db.Books.Find(id);
            ViewData["ISBN"] = b1.ISBN;
            ViewData["bookName"] = b1.bookName;
            ViewData["addTime"] = DateTime.Now;
            return View();
        }

        [HttpPost]
        public ActionResult AddBorrow(Borrow newBorrow)
        {
            string a = Request.Form["ISBN"];
            string x = Request.Form["userName"];
            Book b1 = db.Books.Single(c=>c.ISBN == a);
            Manager m1 = db.Manager.Single(d => d.managerName == x);
            if (ModelState.IsValid)
            {
                Borrow b = new Borrow();
                b.ISBN = Request.Form["ISBN"];
                b.bookName = Request.Form["bookName"];
                b.userName  = Request.Form["userName"];
                b.addTime = DateTime.Parse(Request.Form["addTime"]);
                b.editor  = Request.Form["editor"];
                b.status = 0;                
                b.Phone = m1.phone;
                db.Borrows.Add(b); 
                b1.count--;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            else
                return View(newBorrow);
        }

        public ActionResult Breturn(int id)
        {
            Book b1 = db.Books.Find(id);
            Borrow c = db.Borrows.Single(b=>b.ISBN == b1.ISBN);
            if (c == null)
                return RedirectToAction("Index");
            return View(c);
        }

        [HttpPost]
        public ActionResult Breturn(int id, FormCollection collection)
        {
            var b1 = db.Books.Find(id);
            Borrow c = db.Borrows.Single(b => b.ISBN == b1.ISBN);
            c.status = 1;
            b1.count++;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}
